






















/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
      Room youmeng,fuyun,linghu,wumingu,guoling,shengwuling,yuexiagu,xiaoyao;
       Room wudao,angu,guidi,yixiantian;
       
       // initialise room exits
      youmeng=new Room("深渊迷宫之外, 赶紧寻找龙珠吧!");
      fuyun=new Room("浮云涧", new Monster("陷阱", 10));
      linghu=new Room("灵湖");
      wumingu=new Room("无名谷");
      guoling=new Room("果林",new Thing("果实", 30, 0));
     
      shengwuling=new Room("生物林");
      yuexiagu=new Room("月峡谷",new Thing("龙珠", 0, 0));
      xiaoyao=new Room("逍遥堂",new Thing("辣条", 20, 0));
      wudao=new Room("雾岛");
      angu=new Room("暗谷");
      guidi=new Room("鬼地",new Thing("果实", 30, 0));
      yixiantian=new Room("一线天");
  
       
       currentRoom = youmeng;  // start game outside
        youmeng.setExit("south",fuyun);
       youmeng.setExit("east",linghu);
       youmeng.setExit("west",yixiantian);
       
       fuyun.setExit("east", shengwuling);
       fuyun.setExit("north",youmeng);
       fuyun.setExit("south",xiaoyao);
       
       xiaoyao.setExit("north",fuyun);
       xiaoyao.setExit("west",wudao);
       xiaoyao.setExit("east",yuexiagu);
       
       yixiantian.setExit("east",youmeng);
       yixiantian.setExit("south",angu);
       yixiantian.setExit("west",guoling);
       
       angu.setExit("north",yixiantian);
       angu.setExit("west",guidi);
       angu.setExit("south",wudao);
       
       wudao.setExit("north",angu);
       wudao.setExit("east",xiaoyao);
       
       guidi.setExit("east",angu);
       
       linghu.setExit("west",youmeng);
       linghu.setExit("east",wumingu);
       linghu.setExit("south",shengwuling);
       
       shengwuling.setExit("north",linghu);
       shengwuling.setExit("west",fuyun);
       shengwuling.setExit("east",guoling);
       
       yuexiagu.setExit("west",xiaoyao);
       
       wumingu.setExit("west",linghu);
       wumingu.setExit("south",guoling);
       
       guoling.setExit("north",wumingu);
       guoling.setExit("west",shengwuling);
       guoling.setExit("east",yixiantian);
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("感谢你对该游戏的尝试.  再见!");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到新世界!");
       
        System.out.println("如果需要帮助，请键入“帮助”。");
        System.out.println();
        System.out.println("游戏背景: ");
        System.out.println("你是一个屌丝，你想变帅！");
        System.out.println("你听说迷宫里有龙珠可以实现你的愿望。。。");
        System.out.println("所以你就去了迷宫，加油吧!");
        System.out.println();
        System.out.println("你现在身处" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        Thing thing = currentRoom.getThing();

        if(command.isUnknown()) {
            System.out.println("没有该操作行为");
            return false;
        }
 
        Word commandWord = command.getCommandWord();
        // if (commandWord.equals("help")) {
            // printHelp();
        // }
        // else if (commandWord.equals("go")) {
            // goRoom(command);
        // }
        // else if (commandWord.equals("look")) {
            // say(currentRoom);
        // }
        // else if (commandWord.equals("pick")) {
            // player.addToBag(thing);
            // currentRoom.remove();
        // }
        // else if (commandWord.equals("check")) {
            // player.checkAndPrint();
        // }
        // else if (commandWord.equals("eat")) {
            // player.eatThing(command);
        // }
        // else if (commandWord.equals("quit")) {
            // wantToQuit = quit(command);
        // }
        switch(commandWord) {
            case HELP: 
                printHelp();
                break;
            case GO:
                goRoom(command);
                break;
            case LOOK:
                say(currentRoom);
                break;
            case PICK:
                player.addToBag(thing); 
                currentRoom.remove();
                break;
            case CHECK:
                player.checkAndPrint();
                break;
            case EAT:
                player.eatThing(command);
                break;
            case USE:
                use(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
        }
        return wantToQuit;
    }
    
    private void say(Room currentRoom) {
        if (null == currentRoom.getThing() && null == currentRoom.getMonster()) {
            System.out.println("这个房间内空空如也");
        }
        else if (null != currentRoom.getThing() && null != currentRoom.getMonster()) {
            System.out.println("这个房间内有" + currentRoom.getThing().getName() + "!");
            System.out.println("但是, 野生动物发现了你");
            meet();
        }
        else if (null == currentRoom.getThing()) {
            meet();
        }
        
        else {
            System.out.println("这个房间内有" + currentRoom.getThing().getName() + "!");
        }
    }
    
    private void meet() {
        System.out.println("你遭遇到了"+ currentRoom.getMonster().getMonsterName() +", 很不幸, 你掉入该陷阱");
            player.hurt(currentRoom.getMonster().getDamage());
            System.out.println("该陷阱会让人感到自卑,你因为颜值过低，所以受伤");
            System.out.println("你的体力下降了" + currentRoom.getMonster().getDamage() + " 点!");
            System.out.println("你的体力下降到" + player.getStrength() + "点!");
    }
    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你现在的操作如下:");
        System.out.println("   go --前往下一个房间(要在后面追加方向),每前进一次,便消耗10点体力");
        System.out.println("   help --获取游戏帮助");
        System.out.println("   look -- 找东西");
        System.out.println("   !注意: 有的房间设有陷阱,要小心");
        System.out.println("   pick -- 拾起东西)");
        System.out.println("   check -- 查看背包中的物品");
        System.out.println("   eat -- 吃背包中的补给品");
        
        
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);
        
        if (nextRoom == null) {
            System.out.println("这边无法进入");
        }
        else {
            player.step();
            System.out.println("您消耗了10点体力, "+"您目前的体力是: "+player.getStrength());
            currentRoom = nextRoom;
            System.out.println("你来到了" + currentRoom.getDescription());
            currentRoom.printExits();
        }
    }
    
    private void use(Command command) {
        if(!command.hasSecondWord()) {
            System.out.println("干啥?");
            return;
        }
        
        String key = command.getSecondWord();
        player.use(key);
    }
    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("你要退出的是?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
}
